package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.exception.CustomException;
import com.springboot_gradrecruit.mapper.StudentMapper;
import com.springboot_gradrecruit.model.dto.student.AddResumeRequest;
import com.springboot_gradrecruit.model.dto.student.PageStudentRequest;
import com.springboot_gradrecruit.model.dto.student.UpdateStudentRequest;
import com.springboot_gradrecruit.model.entity.Student;
import com.springboot_gradrecruit.model.entity.User;
import com.springboot_gradrecruit.model.vo.student.StudentVO;
import com.springboot_gradrecruit.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
* @author zr
* @description 针对表【student】的数据库操作Service实现
* @createDate 2024-10-27 19:20:26
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    //添加学生信息
    @Override

    public Long addResume(@RequestBody AddResumeRequest addResumeRequest, HttpServletRequest request) {
        //1.获取参当前登录用户
        User user =(User) request.getSession().getAttribute(addResumeRequest.getUserRole()+ addResumeRequest.getAccount());
        //2.检测当前用户是否有学生信息
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_user_id",user.getUserId());
        if(this.count(queryWrapper)>0){
            throw new CustomException("当前用户已有学生信息");
        }
        //3.添加学生信息
        Student student = new Student();
        student.setStudentUserId(user.getUserId());
        student.setStudentCertificate(addResumeRequest.getStudentCertificate());
        student.setStudentCertificateFile(String.join(",",addResumeRequest.getStudentCertificateFile()));
        student.setStudentCertificationStatus(addResumeRequest.getStudentCertificationStatus());
        student.setStudentName(addResumeRequest.getStudentName());
        student.setStudentImgurl(addResumeRequest.getStudentImgurl());
        student.setStudentSex(addResumeRequest.getStudentSex());
        student.setStudentAge(addResumeRequest.getStudentAge());
        student.setStudentContact(addResumeRequest.getStudentContact());
        student.setStudentEmail(addResumeRequest.getStudentEmail());
        student.setStudentAddress(addResumeRequest.getStudentAddress());
        student.setStudentGraduation(addResumeRequest.getStudentGraduation());
        student.setStudentHighestEducation(addResumeRequest.getStudentHighestEducation());
        student.setStudentMajor(addResumeRequest.getStudentMajor());
        student.setStudentGraduationYear(addResumeRequest.getStudentGraduationYear());
        student.setStudentGpa(addResumeRequest.getStudentGpa());
        student.setStudentInternshipCompany(addResumeRequest.getStudentInternshipCompany());
        student.setStudentKeyResponsibilities(addResumeRequest.getStudentKeyResponsibilities());
        student.setStudentYearWork(addResumeRequest.getStudentYearWork());
        student.setStudentSkills(addResumeRequest.getStudentSkills());
        student.setStudentHonour(addResumeRequest.getStudentHonour());
        student.setStudentProjects(addResumeRequest.getStudentProjects());
        student.setStudentPersonal(addResumeRequest.getStudentPersonal());
        student.setStudentCampusExperience(addResumeRequest.getStudentCampusExperience());
        return (long) studentMapper.insert(student);
    }

    //根据学生绑定的用户id查询学生信息
    @Override
    public StudentVO queryResumeByStudentUserId(Long studentUserId) {

        //1.构建查询条件
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_user_id",studentUserId);
        //2.执行查询
        Student student =studentMapper.selectOne(queryWrapper);
        //3.返回
        return BeanUtil.copyProperties(student,StudentVO.class);
    }

    @Override
    public PageResponse<StudentVO> pageStudent(PageStudentRequest pageStudentRequest) {
        //1.获取参数
        String studentName = pageStudentRequest.getStudentName();
        Integer studentStatus = pageStudentRequest.getStudentStatus();
        String studentSex = pageStudentRequest.getStudentSex();
        Integer studentAge = pageStudentRequest.getStudentAge();
        String studentContact = pageStudentRequest.getStudentContact();
        String studentHighestEducation = pageStudentRequest.getStudentHighestEducation();
        String studentGraduation = pageStudentRequest.getStudentGraduation();
        String studentMajor = pageStudentRequest.getStudentMajor();
        Date studentGraduationYear = pageStudentRequest.getStudentGraduationYear();
        String studentGpa = pageStudentRequest.getStudentGpa();
        String studentCertificationStatus = pageStudentRequest.getStudentCertificationStatus();


        //2.构建分页条件
        Page<Student> page = pageStudentRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Student> studentPage = lambdaQuery()
                .like(StrUtil.isNotBlank(studentName), Student::getStudentName, studentName)
                .like(studentStatus!=null, Student::getStudentStatus, studentStatus)
                .eq(StrUtil.isNotBlank(studentSex), Student::getStudentSex, studentSex)
                .eq(studentAge!=null, Student::getStudentAge, studentAge)
                .like(StrUtil.isNotBlank(studentContact), Student::getStudentContact, studentContact)
                .like(StrUtil.isNotBlank(studentHighestEducation), Student::getStudentHighestEducation, studentHighestEducation)
                .like(StrUtil.isNotBlank(studentGraduation), Student::getStudentGraduation, studentGraduation)
                .like(StrUtil.isNotBlank(studentMajor), Student::getStudentMajor, studentMajor)
                .like(studentGraduationYear!=null, Student::getStudentGraduationYear, studentGraduationYear)
                .like(StrUtil.isNotBlank(studentGpa), Student::getStudentGpa, studentGpa)
                .eq(StrUtil.isNotBlank(studentCertificationStatus), Student::getStudentCertificationStatus, studentCertificationStatus)
                .page(page);
        return PageResponse.of(studentPage, StudentVO.class);
    }

    @Override
    public boolean updateStudentById(UpdateStudentRequest updateStudentRequest) {
        //1.获取参数
        Long studentId = updateStudentRequest.getStudentId();
        String studentCertificate = updateStudentRequest.getStudentCertificate();
        String studentCertificateFile = updateStudentRequest.getStudentCertificateFile();
        String studentCertificationStatus = updateStudentRequest.getStudentCertificationStatus();
        String studentCertificationRefuse = updateStudentRequest.getStudentCertificationRefuse();
        String studentName = updateStudentRequest.getStudentName();
        String studentImgurl = updateStudentRequest.getStudentImgurl();
        String studentSex = updateStudentRequest.getStudentSex();
        Integer studentAge = updateStudentRequest.getStudentAge();
        String studentContact = updateStudentRequest.getStudentContact();
        String studentEmail = updateStudentRequest.getStudentEmail();
        String studentAddress = updateStudentRequest.getStudentAddress();
        String studentHighestEducation = updateStudentRequest.getStudentHighestEducation();
        String studentGraduation = updateStudentRequest.getStudentGraduation();
        String studentMajor = updateStudentRequest.getStudentMajor();
        Date studentGraduationYear = updateStudentRequest.getStudentGraduationYear();
        String studentGpa = updateStudentRequest.getStudentGpa();
        String studentCampusExperience = updateStudentRequest.getStudentCampusExperience();
        String studentInternshipCompany = updateStudentRequest.getStudentInternshipCompany();
        String studentKeyResponsibilities = updateStudentRequest.getStudentKeyResponsibilities();
        String studentYearWork = updateStudentRequest.getStudentYearWork();
        String studentSkills = updateStudentRequest.getStudentSkills();
        String studentHonour = updateStudentRequest.getStudentHonour();
        String studentProjects = updateStudentRequest.getStudentProjects();
        String studentPersonal = updateStudentRequest.getStudentPersonal();

        //2.更新学生信息
        LambdaUpdateWrapper<Student> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Student::getStudentId, studentId)
                .set(StrUtil.isNotBlank(studentCertificate), Student::getStudentCertificate, studentCertificate)
                .set(StrUtil.isNotBlank(studentCertificateFile), Student::getStudentCertificateFile, studentCertificateFile)
                .set(StrUtil.isNotBlank(studentCertificationStatus), Student::getStudentCertificationStatus, studentCertificationStatus)
                .set(StrUtil.isNotBlank(studentCertificationRefuse), Student::getStudentCertificationRefuse, studentCertificationRefuse)
                .set(StrUtil.isNotBlank(studentName), Student::getStudentName, studentName)
                .set(StrUtil.isNotBlank(studentImgurl), Student::getStudentImgurl, studentImgurl)
                .set(StrUtil.isNotBlank(studentSex), Student::getStudentSex, studentSex)
                .set(studentAge!=null, Student::getStudentAge, studentAge)
                .set(StrUtil.isNotBlank(studentContact), Student::getStudentContact, studentContact)
                .set(StrUtil.isNotBlank(studentEmail), Student::getStudentEmail, studentEmail)
                .set(StrUtil.isNotBlank(studentAddress), Student::getStudentAddress, studentAddress)
                .set(StrUtil.isNotBlank(studentHighestEducation), Student::getStudentHighestEducation, studentHighestEducation)
                .set(StrUtil.isNotBlank(studentGraduation), Student::getStudentGraduation, studentGraduation)
                .set(StrUtil.isNotBlank(studentMajor), Student::getStudentMajor, studentMajor)
                .set(studentGraduationYear!=null, Student::getStudentGraduationYear, studentGraduationYear)
                .set(StrUtil.isNotBlank(studentGpa), Student::getStudentGpa, studentGpa)
                .set(StrUtil.isNotBlank(studentCampusExperience), Student::getStudentCampusExperience, studentCampusExperience)
                .set(StrUtil.isNotBlank(studentInternshipCompany), Student::getStudentInternshipCompany, studentInternshipCompany)
                .set(StrUtil.isNotBlank(studentKeyResponsibilities), Student::getStudentKeyResponsibilities, studentKeyResponsibilities)
                .set(StrUtil.isNotBlank(studentYearWork), Student::getStudentYearWork, studentYearWork)
                .set(StrUtil.isNotBlank(studentSkills), Student::getStudentSkills, studentSkills)
                .set(StrUtil.isNotBlank(studentHonour), Student::getStudentHonour, studentHonour)
                .set(StrUtil.isNotBlank(studentProjects), Student::getStudentProjects, studentProjects)
                .set(StrUtil.isNotBlank(studentPersonal), Student::getStudentPersonal, studentPersonal);
        return studentMapper.update(updateWrapper)>0;

    }


}




